<?php

class Folder extends BaseFolder
{
	
public function __toString()
  {
    return $this->name;
  }
  
  public function getCount()
  {
  	if($this->count=='') return '0';
  	return $this->count;
  }
  
public function retrivePopImage($folder_id)
  {

  	$c = new Criteria();
  	$c->add(MaterialPeer::TYPE_ID,5);
  	$c->addJoin(MaterialPeer::ID,Mat2FolderPeer::MATERIAL_ID);
  	$c->addJoin(Mat2FolderPeer::FOLDER_ID,FolderPeer::ID);
  	$c->addAnd(FolderPeer::ID,$this->id);
  	$c->addAnd(MaterialPeer::RATING,100,Criteria::GREATER_THAN);
  	
  	////привязка по регионам
  		   if(sfConfig::get('app_use_region')==true)
        {
        	
        	if(sfContext::getInstance()->getUser()->getAttribute('city_id')!=1)
        	{
				$crit1=$c->getNewCriterion(MaterialPeer::REGION_ID,sfContext::getInstance()->getUser()->getAttribute('city_id'));
				$crit2=$c->getNewCriterion(MaterialPeer::REGION_ID,1);
				$crit1->addOr($crit2);  
        		$c->add($crit1); 
        	}
        }
    /////

        
  	$im = MaterialPeer::doSelectOne($c);
  	if($im)
  	{
  	return $im->getLinkToCompanySmall($folder_id);	
  	}
  	else 
  	{
  	return '';	
  	}
    
  }
  
  public function getScopeOrg()
  {
  	return '3';
  }

  
  public function getTreearray()
  {
  	$arr=array();
    return $this->getTreerec($arr);
  }
  
    public function getTreerec($arr)
  {
     if($this->hasParent())
     {
     	$arr[] = $this->getParent();
     }	
  }
  
  /*
   * Обновляем теги
   */
  public function refreshTag()
  {

  	 $c = new Criteria();
  	 $c->add(Mat2FolderPeer::FOLDER_ID,$this->id);
  	 $m = Mat2FolderPeer::doSelect($c);
  	 if($m)
  	 {
  	 	
  	 	foreach($m as $m)
  	 	{
  	 		$arr = array();
  	 		$str = $m->getTag();
  	 		$arr = split(' ## ',$str);
  	 		
  	 		
  	 		if(!is_array($arr))
  	 		{
  	 			$str = $m->getTag().' ## '.$this->description;
  	 		}
  	 		else
  	 		{
  	 		    $str = $arr[0].' ## '.$this->description;	
  	 		}
  	 		$m->setTag($str);
  	 		$m->save();
  	 	}
  	 }
  	
  }
  
  
  
}

 $columns_map = array('left'   => FolderPeer::TREE_LEFT,
                           'right'  => FolderPeer::TREE_RIGHT,
                           'parent' => FolderPeer::TREE_PARENT,
                           'scope'  => FolderPeer::SCOPE);
 
      sfPropelBehavior::add('Folder', array('actasnestedset' => array('columns' => $columns_map)));
